﻿#region Using Statemens
using System;
using GameBase.Screens;
using GameBase.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StarFox.ScreenComponents;
using StarFox.Sounds;
#endregion

namespace StarFox.Screens
{
    /// <summary>
    /// PEGI info screen of the game.
    /// </summary>
    public class PegiScreen : StarFoxScreen
    {
        #region Initialization

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screenManager">The screen manager that this screen is associated to.</param>
        public PegiScreen(ScreenManager screenManager)
            : base(screenManager)
        {
            Name = "PegiScreen";
        }

        /// <summary>
        /// Called when the Screen needs to be initialized.
        /// </summary>
        public override void Initialize()
        {
            Components2D.Add(new PegiMessage(this));

            base.Initialize();
        }

        #endregion
    }

    #region PEGI Message

    /// <summary>
    /// PEGI message.
    /// </summary>
    public class PegiMessage : ScreenComponent2D
    {
        #region Fields

        // sprite sheet
        SpriteSheet spriteSheet;

        // PEGI logo
        Rectangle dstRectPegi;
        bool toPegi = false;
        bool playVoice = true;
        TimeSpan totalPegiTime = TimeSpan.FromMilliseconds(2000);

        // PEGI voice message
        ScreenSound sound;

        #endregion

        #region Screen Transitions

        // indicates whether a transition is taking place
        bool toVisible = false;
        bool toHidden = false;

        // normalized transition time
        float trTime = 0.0f;

        /// <summary>
        /// Makes the ScreenComponent2D transition to visible state.
        /// </summary>
        public override void Show()
        {
            toVisible = true;
        }

        /// <summary>
        /// Makes the ScreenComponent2D transition to hidden state.
        /// </summary>
        public override void Hide()
        {
            toHidden = true;
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screen">The Screen that this ScreenComponent2D is associated to.</param>
        public PegiMessage(StarFoxScreen screen)
            : base(screen)
        {
            // PEGI voice message
            sound = Game.Services.GetService(typeof(ScreenSound)) as ScreenSound;
            if (sound == null)
                throw new InvalidOperationException("Service of type ScreenSound not found");

            // asset loading
            spriteSheet = Screen.Content.Load<SpriteSheet>(ContentSettings.GetAsset("INTRO_SPRITES"));

            // PEGI logo
            Rectangle auxRect = spriteSheet.SourceRectangle("PEGI");
            dstRectPegi = new Rectangle(0, 0, (int)(auxRect.Width * Screen.Scale), (int)(auxRect.Height * Screen.Scale));
            dstRectPegi.Location = new Point((int)((Screen.Width - dstRectPegi.Width) * 0.5f), (int)((Screen.Height - dstRectPegi.Height) * 0.5f));
        }

        #endregion

        #region Update

        /// <summary>
        /// Called when the ScreenComponent2D needs to be updated.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Update(GameTime gameTime)
        {
            #region Screen Transition

            if (toVisible)
            {
                // update transition timer
                float trDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / Time.TotalMilliseconds);
                trTime += trDelta;
                trTime = MathHelper.Clamp(trTime, 0.0f, 1.0f);
                if (trTime == 1.0f) { toVisible = false; toPegi = true; trTime = 0.0f; }
            }
            else if (toHidden)
            {
                // update transition timer
                float trDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / Time.TotalMilliseconds);
                trTime += trDelta;
                trTime = MathHelper.Clamp(trTime, 0.0f, 1.0f);
                if (trTime == 1.0f) { toHidden = false; trTime = 0.0f; }
            }

            #endregion

            #region PEGI

            if (toPegi)
            {
                // PEGI voice message
                if (playVoice) { sound.PEGI3(); playVoice = false; }

                // update PEGI timer
                float trDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / totalPegiTime.TotalMilliseconds);
                trTime += trDelta;
                trTime = MathHelper.Clamp(trTime, 0.0f, 1.0f);
                if (trTime == 1.0f) { toPegi = false; trTime = 0.0f; Screen.ScreenManager.AddScreen(new DevWarningScreen(Screen.ScreenManager)); }
            }

            #endregion
        }

        #endregion

        #region Draw

        /// <summary>
        /// Called when the ScreenComponent2D needs to be drawn.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Draw(GameTime gameTime)
        {
            // PEGI logo
            SpriteBatch.Draw(spriteSheet.Texture, dstRectPegi, spriteSheet.SourceRectangle("PEGI"), Color.White);
        }

        #endregion
    }

    #endregion
}
